package com.sg.business.commons.ui.work;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;

import com.mobnut.db.model.IContext;
import com.mobnut.db.model.PrimaryObject;
import com.sg.widgets.part.CurrentAccountContext;
import com.sg.widgets.part.editor.PrimaryObjectEditorInput;
import com.sg.widgets.part.editor.page.AbstractFormPageDelegator;
import com.sg.widgets.registry.config.BasicPageConfigurator;

public abstract class AbstractEngineeringChangePage extends
		AbstractFormPageDelegator {

	private ScrolledComposite sc;
	private EngineeringChangePanel ecp;
	protected IContext context;

	@Override
	public Composite createPageContent(IManagedForm mForm, Composite parent,
			PrimaryObjectEditorInput input, BasicPageConfigurator conf) {
		context = new CurrentAccountContext();
		parent.setLayout(new FillLayout());
		setFormInput(input);

		sc = new ScrolledComposite(parent, SWT.V_SCROLL);
		ecp = new EngineeringChangePanel(sc, getEngineeringChangeControl()) {

			@Override
			protected IContext getContext() {
				return context;
			}

			@Override
			public boolean canEnableNoticeCreater() {
				return AbstractEngineeringChangePage.this
						.canEnableNoticeCreater();
			}

			@Override
			public boolean canEnableBusinessCreater() {
				return AbstractEngineeringChangePage.this
						.canEnableBusinessCreater();
			}

			@Override
			public boolean canEnableProductCreater() {
				return AbstractEngineeringChangePage.this
						.canEnableProductCreater();
			}

			@Override
			public boolean canEnableWorkListCreater() {
				return AbstractEngineeringChangePage.this
						.canEnableWorkListCreater();
			}

		};
		PrimaryObject data = input.getData();
		ecp.setCanEnabled(getCanEnabled(data));
		ecp.createContent();
		ecp.setInput(data);

		sc.setContent(ecp);

		sc.setExpandHorizontal(true);
		sc.setExpandVertical(true);
		sc.setAlwaysShowScrollBars(true);
		sc.setMinSize(ecp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		sc.addControlListener(new ControlAdapter() {

			@Override
			public void controlResized(ControlEvent e) {
				sc.setMinSize(ecp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
			}
		});
		return sc;
	}

	protected boolean canEnableWorkListCreater() {
		return true;
	}

	protected boolean canEnableProductCreater() {
		return true;
	}

	protected boolean canEnableBusinessCreater() {
		return true;
	}

	protected boolean canEnableNoticeCreater() {
		return true;
	}

	protected abstract boolean getCanEnabled(PrimaryObject data);

	protected abstract int getEngineeringChangeControl();

	@Override
	public void commit(boolean onSave) {
		setDirty(false);
	}

	@Override
	public void setFocus() {
	}

}
